package com.deer.wms.web.controller.ware;


import java.io.File;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.deer.wms.base.system.model.item.ItemInfo;
import com.deer.wms.base.system.model.ware.QRCodeGenerator;
import com.deer.wms.base.system.service.item.IItemInfoService;
import com.deer.wms.framework.util.ShiroUtils;
import com.deer.wms.base.system.model.ware.WareInfo;
import com.deer.wms.base.system.service.ware.IWareInfoService;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.sun.glass.ui.Robot;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.common.annotation.Log;
import com.deer.wms.common.enums.BusinessType;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 仓库设置 信息操作处理
 *
 * @date 2019-05-08
 */
@Controller
@RequestMapping("/system/wareInfo")
public class WareInfoController extends BaseController {
    private String prefix = "system/wareInfo";

    @Autowired
    private IWareInfoService wareInfoService;
    @Autowired
    private IItemInfoService itemInfoService;


    @RequiresPermissions("system:wareInfo:view")
    @GetMapping()
    public String wareInfo() {
        return prefix + "/wareInfo";
    }

    //二维码生成
    @RequestMapping("qr")
    public void QR(HttpServletRequest request, HttpServletResponse response,@RequestParam String ids) throws Exception {


            try {
                 /* WareInfo w=new WareInfo();
            w.setWareId(Integer.parseInt(id));*/


            } catch (Exception e){
                e.printStackTrace();
            }finally {
                WareInfo wareInfo = wareInfoService.findBy("wareId", Integer.parseInt(ids));
                //图片名称
                String name = wareInfo.getWareName()+wareInfo.getWareId();
                //二维码中包含的信息
                String content = wareInfo.getWareCode();
                Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
                // 指定编码格式
                hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
                // 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
                hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
                // 编码内容,编码类型(这里指定为二维码),生成图片宽度,生成图片高度,设置参数
                BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200, hints);
                //设置请求头
                response.setHeader("Content-Type", "application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name + ".png", "UTF-8"));

                /* 设置文件ContentType类型，这样设置，会自动判断下载文件类型 *//*
                response.setContentType("multipart/form-data");
                *//* 设置文件头：最后一个参数是设置下载文件名 *//*
                response.setHeader("Content-Disposition", "attachment;filename="+file.getName());*/


                OutputStream outputStream = response.getOutputStream();

                MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);

                outputStream.flush();
                outputStream.close();
            }



    }


    /**
     * 查询仓库设置列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WareInfo wareInfo) {
        startPage();
        List<WareInfo> list = wareInfoService.selectWareInfoList(wareInfo);
        return getDataTable(list);
    }

    /**
     * 导出仓库设置列表
     */
    @RequiresPermissions("system:wareInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WareInfo wareInfo) {
        List<WareInfo> list = wareInfoService.findExport(wareInfo);
        ExcelUtil<WareInfo> util = new ExcelUtil<WareInfo>(WareInfo.class);
        return util.exportExcel(list, "仓库信息");
    }

    /**
     * 新增仓库设置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存仓库设置
     */
    @RequiresPermissions("system:wareInfo:add")
    @Log(title = "仓库设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WareInfo wareInfo) {
        if (wareInfo.getWareCode() == null || wareInfo.getWareCode() == "" || wareInfo.getWareName() == null || wareInfo.getWareName() == "") {
            return error("请填写仓库编码或仓库名称！");
        }
//		WareInfo wareInfo1 = new WareInfo();
//		wareInfo1.setWareId(wareInfo.getWareId());
//		wareInfo1.setWareCode(wareInfo.getWareCode());
//		wareInfo1.setWareName(wareInfo.getWareName());
//		List<WareInfo> w1 = wareInfoService.findWareInfoList(wareInfo1);
////		if(w1!=null){
//		if(w1.size()>0){
//			return error("仓库名已存在!");
//		}

        wareInfo.setCreateUserId(ShiroUtils.getUserId());
        wareInfo.setCreateUserName(ShiroUtils.getLoginName());
        wareInfoService.insertWareInfo(wareInfo);
        return success();
    }

    /**
     * 修改仓库设置
     */
    @GetMapping("/edit/{wareId}")
    public String edit(@PathVariable("wareId") Integer wareId, ModelMap mmap) {
        WareInfo wareInfo = wareInfoService.selectWareInfoById(wareId);
        mmap.put("wareInfo", wareInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存仓库设置
     */
    @RequiresPermissions("system:wareInfo:edit")
    @Log(title = "仓库设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WareInfo wareInfo) {
        if (wareInfo.getWareCode() == null || wareInfo.getWareCode() == "" || wareInfo.getWareName() == null || wareInfo.getWareName() == "") {
            return error("请填写仓库编码或仓库名称！");
        }
//		WareInfo wareInfo1 = new WareInfo();
//		wareInfo1.setWareId(wareInfo.getWareId());
//		wareInfo1.setWareCode(wareInfo.getWareCode());
//		wareInfo1.setWareName(wareInfo.getWareName());
//		List<WareInfo> w1 = wareInfoService.findWareInfoList(wareInfo1);
//		if(w1.size()>0){
//			return error("仓库名已存在!");
//		}
//		WareInfo wareInfo2 = wareInfoService.findById(212);
//		if(wareInfo.getBoxHeight()<wareInfo2.getBoxHeight()){
//			return error("当前高度需大于原有高度!");
//		}
//		else if(wareInfo.getBoxHeight()>wareInfo2.getBoxHeight()){
//			List<ItemInfo> itemInfos = itemInfoService.selectItemInfoList(new ItemInfo());
//			if(itemInfos.size()>0){
//				for(ItemInfo itemInfo : itemInfos) {
//					Integer maxPackQuantity = calcuateStoreQuantity(itemInfo.getThickness(),wareInfo.getBoxHeight());
//					itemInfo.setMaxPackQty(maxPackQuantity);
//					itemInfoService.update(itemInfo);
//				}
//			}
//		}
        return toAjax(wareInfoService.updateWareInfo(wareInfo));
    }

    /**
     * 删除仓库设置
     */
    @RequiresPermissions("system:wareInfo:remove")
    @Log(title = "仓库设置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wareInfoService.deleteWareInfoByIds(ids));
    }

    private Integer calcuateStoreQuantity(Double thickness, Integer wareBoxHeight) {
        BigDecimal boxHeight = new BigDecimal(wareBoxHeight);
        BigDecimal itemHeight = new BigDecimal(Double.toString(thickness));
        BigDecimal divide = boxHeight.divide(itemHeight, 1, BigDecimal.ROUND_DOWN);
        String replace = Integer.toString(divide.intValue()).substring(0, Integer.toString(divide.intValue()).length() - 1);
        return Integer.parseInt(replace + "0");
    }
}
